/**********************************************************************
 Copyright (C) 2009 Jaakko Vuori

 This file is part of Cimer.
 For more information, see <http://code.google.com/p/cimer/>

 Cimer is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Cimer is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Cimer.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "keyeventhandler.h"
#include <QKeyEvent>
#include "piewidget.h"

const int KFirstStepInterval = 120;
const int KAdjustInterval = 25;

KeyEventHandler::KeyEventHandler(PieWidget* pieWidget, QObject* parent) :
    QObject(parent), m_adjustDirection(None), m_pieWidget(pieWidget)

{
    connect(&m_keyTimer, SIGNAL(timeout()), this, SLOT(timerEvent()));

}

void KeyEventHandler::stop()
{
    m_keyTimer.stop();
}

void KeyEventHandler::timerEvent()
{
    // The first step of adjustment takes a bit longer so it's more easier to adjust exactly one minute.
    if (m_keyTimer.interval() == KFirstStepInterval)
        {
        m_keyTimer.setInterval(KAdjustInterval);
        }

    switch (m_adjustDirection)
    {
        case Increasing:
            m_pieWidget->adjustTime(1);
            break;
        case Decreasing:
            m_pieWidget->adjustTime(-1);
            break;
        default:
            break;
    }
}

void KeyEventHandler::handleKeyPressEvent(QKeyEvent* event)
{
    AdjustDirection direction = None;
    switch (event->key())
    {
        case '-':
#ifdef Q_OS_SYMBIAN
        case Qt::Key_Left:
        case Qt::Key_Up:
#endif
            direction = Decreasing;
            break;
        case '+':
#ifdef Q_OS_SYMBIAN
        case Qt::Key_Right:
        case Qt::Key_Down:
#endif
            direction = Increasing;
            break;
        default:
            break;
    }

    if (direction == Decreasing)
    {
        if ( m_adjustDirection != Decreasing )
        {
            m_adjustDirection = Decreasing;
            m_keyTimer.start(KFirstStepInterval);
            // Remove one minute immediately when the key is pressed
            m_pieWidget->adjustTime(-1);
            m_pieWidget->setPaused(true);
        }
    }
    else if (direction == Increasing)
    {
        if ( m_adjustDirection != Increasing )
        {
            m_keyTimer.start(KFirstStepInterval);
            m_adjustDirection = Increasing;
            // Add one minute immediately when the key is pressed
            m_pieWidget->adjustTime(1);
            m_pieWidget->setPaused(true);
        }
    }
    else
    {
        m_keyTimer.stop();
        m_adjustDirection = None;
        m_pieWidget->setPaused(false);
    }
}

void KeyEventHandler::handleKeyReleaseEvent(QKeyEvent* event)
{
    Q_UNUSED(event);

    if ( m_adjustDirection != None)
    {
        m_keyTimer.stop();
        m_adjustDirection = None;
        m_pieWidget->setPaused(false);
    }
}
